package com.neuedu.web.service;

import com.neuedu.web.dao.UploadDao;
import com.neuedu.web.po.UmsUser;
import com.neuedu.web.po.UploadPO;
import com.neuedu.web.utils.StringUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @author 金山
 * 项目：his-server
 * site: https://blog.fulfill.com.cn
 * 描述   上传，下载
 * @data 2024/12/69:00
 */
public class UploadService {

    private UploadDao uploadDao = new UploadDao();

    public static final  File dir = new File("D:\\upload");



   public UploadPO upload(HttpServletRequest request , HttpServletResponse response ) throws Exception {

       HttpSession session  = request.getSession();
       UmsUser user = (UmsUser) session.getAttribute("user");


       ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
       upload.setHeaderEncoding("utf-8");


       List<FileItem> fileItems = upload.parseRequest(request);
       FileItem fileItem = fileItems.get(0);

       boolean formField = fileItem.isFormField();  //是 input     FIle

       //   input type="file" name="file" value="微信图片_20241017140004.gif"
       String name = fileItem.getName();   //上传文件的原始名称
       String fieldName = fileItem.getFieldName();
       long size = fileItem.getSize();
       String ext = name.substring(name.lastIndexOf("."));
       String saveFileName = StringUtils.uuid()+ext;
       // String username = fileItem.getString("username");

       System.out.println("formField = " + formField);
       System.out.println("name = " + name);
       System.out.println("fieldName = " + fieldName);
       System.out.println("size = " + size);
       fileItem.write(new File(dir,saveFileName));


       UploadPO po = new UploadPO();
       po.setOriginName(name);//原始文件名称name
       po.setExt(ext);
       po.setPath(saveFileName);
       po.setUpTime(new Date());
       po.setUpUser(user.getUsername());

       uploadDao.insert(po);


       //存储上传文件
       return po;
   }


    /**
     * 文件下载
     * @param request
     * @param response
     * @throws Exception
     */
    public void download(HttpServletRequest request , HttpServletResponse response ) throws Exception {


       String uploadId = request.getParameter("uploadId");

        UploadPO uploadPO = uploadDao.selectById(Long.valueOf(uploadId));

        ServletOutputStream os = response.getOutputStream();
        //下载文件
        File  dist = new File(dir,uploadPO.getPath());
        InputStream is = new FileInputStream(dist);

        byte[] buffer = new byte[1024];
        int len = -1;

        while (     ( len = is.read(buffer))  != -1       ){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();

    }

}
